#include "GrammarChecker.h"

GrammarChecker::GrammarChecker(){
    _mapMoves= new QMap<int, QVector<int>>();
    _mapToken = new QMap<QString, int>;
}
void GrammarChecker::addToken(QString * tokens){
    int indexMap=0;
    QStringList listTok = (*tokens).split(",", QString::SkipEmptyParts);
    foreach(QString token, listTok) {
        indexMap++;
        (*_mapToken)[token]=indexMap;
    }
}
void GrammarChecker::addMoveRule(QString moveRule){

    QStringList list1 = moveRule.split("->", QString::SkipEmptyParts);
    QStringList list2 = list1[1].split("|", QString::SkipEmptyParts);
    QVector<int> nextToken;
    foreach(QString token, list2) {
        nextToken.append((*_mapToken)[token]);
    }
    (*_mapMoves)[(*_mapToken)[list1[0]]]=nextToken;

}
bool GrammarChecker::checkMove(QString * prevTok, QString * nextTok){
    int preInt = (*_mapToken)[(* prevTok)];
    int nexInt = (*_mapToken)[(* nextTok)];
    QVector<int> nextToken = (*_mapMoves)[preInt];
    foreach(int num, nextToken) {
        if (num==nexInt)
            return true;
    }
    return false;
}
GrammarChecker::~GrammarChecker(){
    //delete _mapToken;
    //delete _mapMoves;
}
